﻿<template>
  <a-drawer
    :destroyOnClose="true"
    :keyboard="true"
    :loading="loading"
    :title="title"
    :visible="visible"
    width="60%"
    @close=" () => { this.visible = false }">
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-row :gutter="10" type="flex">
          <a-col flex="5">
            <a-form-model-item label="排序" prop="Sort">
              <a-input v-model="entity.Sort" autocomplete="off" placeholder="越小越靠前"/>
            </a-form-model-item>
          </a-col>
          <a-col flex="5">
            <a-form-model-item label="错误编码" prop="ErrorCode">
              <a-input v-model="entity.ErrorCode" autocomplete="off" placeholder="错误编码"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="10" type="flex">
          <a-col flex="5">
            <a-form-model-item label="错误类型" prop="ErrorType">
              <a-select v-model="entity.ErrorType" placeholder="错误类型" style="width: 100%">
                <a-select-option v-for="item in ErrorTypeList" :key="item.Id" :value="item.DicName">
                  {{ item.DicName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col flex="5">
            <a-form-model-item label="错误模块" prop="ErrorModule">
              <a-select v-model="entity.ErrorModule" placeholder="错误模块" style="width: 100%">
                <a-select-option v-for="item in ErrorModuleList" :key="item.Id" :value="item.DicName">
                  {{ item.DicName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="10" type="flex">
          <a-col flex="5">
            <a-form-model-item label="中文错误信息" prop="ErrorInfoCN">
              <a-textarea v-model="entity.ErrorInfoCN" autocomplete="off" placeholder="中文错误信息" :rows="5"/>
            </a-form-model-item>
          </a-col>
          <a-col flex="5">
            <a-form-model-item label="英文错误信息" prop="ErrorInfoEN">
              <a-textarea v-model="entity.ErrorInfoEN" autocomplete="off" placeholder="英文错误信息" :rows="5"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="10" type="flex">
          <a-col flex="5">
            <a-form-model-item label="错误原因" prop="ErrorReason">
              <a-textarea v-model="entity.ErrorReason" autocomplete="off" placeholder="错误原因" :rows="5"/>
            </a-form-model-item>
          </a-col>
          <a-col flex="5">
            <a-form-model-item label="错误解决方案" prop="ErrorSolution">
              <a-textarea v-model="entity.ErrorSolution" autocomplete="off" placeholder="错误解决方案" :rows="5"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="10" type="flex">
          <a-col flex="5">
            <a-form-model-item label="错误解决方案URL" prop="ErrorSolutionURL">
              <a-textarea v-model="entity.ErrorSolutionURL" autocomplete="off" placeholder="错误解决方案URL" :rows="5"/>
            </a-form-model-item>
          </a-col>
          <a-col flex="5">
            <a-form-model-item label="备注" prop="Remark">
              <a-textarea v-model="entity.Remark" autocomplete="off" placeholder="备注" :rows="5"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
    <div :style="{ position: 'absolute', right: 0, bottom: 0, width: '100%', borderTop: '1px solid #e9e9e9', padding: '10px 16px', letterSpacing: '20px', background: '#fff', textAlign: 'center', zIndex: 1 }">
      <a-button @click="close"> 取消</a-button>&nbsp;
      <a-button :loading="loading" type="primary" @click="handleSubmit"> 保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    parentObj: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
      ErrorModuleList: [],
      ErrorTypeList: []
    }
  },
  methods: {
    init () {
      this.visible = true
      this.ErrorTypeList = []
      this.ErrorModuleList = []
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.getErrorType()
      this.getErrorModule()
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/Base_Manage/Base_ErrorManual/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    close () {
      this.visible = false
      this.entity = {}
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_Manage/Base_ErrorManual/SaveData', this.entity).then(resJson => {
          this.loading = false
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.loading = false
            this.visible = false
            this.$emit('returnMain')
          } else {
            this.$message.error(resJson.Msg)
            this.loading = false
          }
        })
      })
    },
    // 获取错误类型字典
    getErrorType () {
      this.$http.post('/Base_BasicData/Base_DictionaryMain/GetTheDataByDicKey', {
        Condition: 'DicKey',
        Keyword: 'ErrorType'
      }).then(res => {
        if (res.Success) {
          this.ErrorTypeList = res.Data.DictionaryDetailsList
        }
      })
    },
    // 获取错误模块字典
    getErrorModule () {
      this.$http.post('/Base_BasicData/Base_DictionaryMain/GetTheDataByDicKey', {
        Condition: 'DicKey',
        Keyword: 'ErrorModule'
      }).then(res => {
        if (res.Success) {
          this.ErrorModuleList = res.Data.DictionaryDetailsList
        }
      })
    },
    // 非空校验
    validEmpty (index, name, tips) {
      return (rule, value, callback) => {
        const data = this.DetaiData[index][name]
        if (!data) {
          this.$message.warning('列表第' + (index + 1) + '行：' + tips)
          callback(tips || '必填')
        } else {
          callback()
        }
      }
    }
  }
}
</script>
<style scoped>
</style>
